%% Author: xiaomai
%% Created: 2010-10-22
%% Description: 18交易信息（点对点）
-module(pt_18).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([read/2, write/2]).
-include("common.hrl").

%%
%%客户端 -> 服务端 ----------------------------
%%

%% -----------------------------------------------------------------
%% 发起交易请求
%% -----------------------------------------------------------------
read(18000, <<BPlayerId:32>>) ->
	{ok, [BPlayerId]};

%% -----------------------------------------------------------------
%% 确定接受交易
%% -----------------------------------------------------------------
read(18002, <<HandleResult:16, APlayerId:32>>) ->
	{ok, [HandleResult, APlayerId]};

%% -----------------------------------------------------------------
%% 交易物品操作
%% -----------------------------------------------------------------
read(18003, <<GoodsType:16, GoodsId:32, GoodsNum:32, BaseGoodsId:32>>) ->
	{ok, [GoodsType, GoodsId, GoodsNum, BaseGoodsId]};

%% -----------------------------------------------------------------
%% 锁定交易
%% -----------------------------------------------------------------
read(18005, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 取消交易
%% -----------------------------------------------------------------
read(18007, _R) ->
	{ok, [1]};

%% -----------------------------------------------------------------
%% 实施交易
%% -----------------------------------------------------------------
read(18008, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 拖出物品操作
%% -----------------------------------------------------------------
read(18012, <<GoodsId:32, GoodsNum:32, BaseGoodsId:32>>) ->
	{ok, [GoodsId, GoodsNum, BaseGoodsId]};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
read(_Cmd, _R) ->
    {error, no_match}.

%%
%%服务端 -> 客户端 ------------------------------------
%%



%% -----------------------------------------------------------------
%% 发起交易请求
%% -----------------------------------------------------------------
write(18000, [Result]) ->
    Data = <<Result:16>>,
    {ok, pt:pack(18000, Data)};

%% -----------------------------------------------------------------
%% 通知被发起方交易请求（服务器主推）
%% -----------------------------------------------------------------
write(18001, [APlayerId, APlayerName, APLv]) ->
	Len = byte_size(APlayerName),
    Data = <<APlayerId:32, Len:16, APlayerName/binary, APLv:16>>,
    {ok, pt:pack(18001, Data)};

%% -----------------------------------------------------------------
%% 确定接受交易
%% -----------------------------------------------------------------
write(18002, [Result]) ->
    Data = <<Result:16>>,
    {ok, pt:pack(18002, Data)};

%% -----------------------------------------------------------------
%% 交易物品操作
%% -----------------------------------------------------------------
write(18003, [Result]) ->
    Data = <<Result:16>>,
    {ok, pt:pack(18003, Data)};

%% -----------------------------------------------------------------
%% 交易物品信息（服务器主推）
%% -----------------------------------------------------------------
write(18004, [GoodsType, GoodsId, GoodsNum, BaseGoodsId]) ->
    Data = <<GoodsType:16, GoodsId:32, GoodsNum:32, BaseGoodsId:32>>,
    {ok, pt:pack(18004, Data)};

%% -----------------------------------------------------------------
%% 交易物品信息（服务器主推）
%% -----------------------------------------------------------------
write(18006, []) ->
    Data = <<>>,
    {ok, pt:pack(18006, Data)};

%% -----------------------------------------------------------------
%% 取消交易
%% -----------------------------------------------------------------
write(18007, []) ->
    Data = <<>>,
    {ok, pt:pack(18007, Data)};

%% -----------------------------------------------------------------
%% 实施交易
%% -----------------------------------------------------------------
write(18008, []) ->
    Data = <<>>,
    {ok, pt:pack(18008, Data)};

%% -----------------------------------------------------------------
%% 双方进入交易状态（服务器主推）
%% -----------------------------------------------------------------
write(18009, [PlayerId, PlayerName]) ->
	Len = byte_size(PlayerName),
	Data = <<PlayerId:32, Len:16, PlayerName/binary>>,
	{ok, pt:pack(18009, Data)};

%% -----------------------------------------------------------------
%% 对方拒绝交易请求（服务器主推）
%% -----------------------------------------------------------------
write(18010, [BPlayerId, BPlayerName]) ->
	Len = byte_size(BPlayerName),
	Data = <<BPlayerId:32, Len:16, BPlayerName/binary>>,
	{ok, pt:pack(18010, Data)};

%% -----------------------------------------------------------------
%% 交易结果 （服务器主推）
%% -----------------------------------------------------------------
write(18011, [Result]) ->
    Data = <<Result:16>>,
    {ok, pt:pack(18011, Data)};

%% -----------------------------------------------------------------
%% 拖出物品操作,返回结果
%% -----------------------------------------------------------------
write(18012, [Result]) ->
    Data = <<Result:16>>,
    {ok, pt:pack(18012, Data)};

%% -----------------------------------------------------------------
%% 拖出物品操作（服务器主推）
%% -----------------------------------------------------------------
write(18013, [GoodsId, GoodsNum, BaseGoodsId]) ->
    Data = <<GoodsId:32, GoodsNum:32, BaseGoodsId:32>>,
    {ok, pt:pack(18013, Data)};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
write(Cmd, _R) ->
?INFO_MSG("~s_errorcmd_[~p] ",[misc:time_format(yg_timer:now()), Cmd]),
    {ok, pt:pack(0, <<>>)}.